package cn.yunyichina.provider.framework.annotation.validate;

import java.lang.reflect.Field;
import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Validate the @ValidateNotNull annotation
 *
 * @author jiangtch
 */
public class ValidateConditionNotNullAndNotEmptyHandler implements Handler {

    private static final Logger logger = LoggerFactory.getLogger(ValidateNotNullHandler.class);

    public void validate(AnnotationValidable validatedObj, Field field)
            throws ValidateException {
        if (logger.isTraceEnabled()) {
            logger.trace(this.getClass().getName(), "validate()");
        }
        if (field.isAnnotationPresent(ValidateConditionNotNullAndNotEmpty.class)) {
            checkConditionNotNullAndNotEmpty(validatedObj, field);
        }
    }

    private void checkConditionNotNullAndNotEmpty(AnnotationValidable filter, Field field)
            throws ValidateException {
        if (logger.isTraceEnabled()) {
            logger.trace(this.getClass().getName(), "checkConditionNotNullAndNotEmpty()");
        }
        ValidateConditionNotNullAndNotEmpty annotation = field.getAnnotation(ValidateConditionNotNullAndNotEmpty.class);
        String values = annotation.value();
        String[] conditions = values.split(",");

        boolean isNeedCheck = true;
        if (conditions.length == 0) {
            isNeedCheck = false;
        }

        Object dest;
        try {
            dest = GetFiledValue.getField(filter, field.getName());
            for (String c : conditions) {
                try {
                    String conditionField = c.split("=")[0].trim();
                    String consitionValue = c.split("=")[1].trim();
                    String conditionClassValue = String.valueOf(GetFiledValue.getField(filter, conditionField));
                    if (!consitionValue.equals(conditionClassValue)) {
                        isNeedCheck = false;
                        break;
                    }
                } catch (Exception e) {
                    logger.error("字段：" + field.getName() + " 在条件字段:" + c + " 获取条件值出错，" + e.getMessage(), e);
                    throw new ValidateException("字段：" + field.getName() + " 在条件字段:" + c + " 获取条件值出错，" + e.getMessage(), e);
                }
            }

        } catch (Exception ex) {
            logger.error("字段：" + field.getName() + " 获取值发生异常->" + ex.getMessage(), ex);
            throw new ValidateException("字段：" + field.getName() + " 获取值发生异常->" + ex.getMessage(), ex);
        }

        if (isNeedCheck) {

            if (dest == null) {
                logger.warn("字段：" + field.getName() + " 在以下条件下:" + values + "不能为空值，请检查!");
                throw new ValidateException("字段：" + field.getName() + " 在以下条件下:" + values + "不能为空值，请检查!");
            }

            if (dest instanceof String) {
                if (((String) dest).length() == 0) {
                    logger.warn("字段：" + field.getName() + " 在以下条件下:" + values + "不能为空字符串，请检查!");
                    throw new ValidateException("字段：" + field.getName() + " 在以下条件下:" + values + "不能为空字符串，请检查!");
                }
            } else if (dest instanceof Collection) {
                if (((Collection) dest).size() == 0) {
                    logger.warn("字段：" + field.getName() + " 在以下条件下:" + values + "不能为空集合，请检查!");
                    throw new ValidateException("字段：" + field.getName() + " 在以下条件下:" + values + "不能为空集合，请检查!");
                }
            }
        }
    }

}
